package cc.mrbird.febs.common.configure;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cc.mrbird.febs.common.interceptor.ClientSignInterceptor;
import cc.mrbird.febs.common.interceptor.ClientTokenInterceptor;

/**
 *  客户端登录配置
 *  “ClientSecurityConfig”类实现“WebMvcConfigurer”，
 *  重新“addInterceptors”方法，其目的是设置拦截规则，
 *  excludePathPatterns为需要排除的规则，addPathPatterns为需要拦截的规则。
 */
@Configuration
public class ClientSecurityConfig implements WebMvcConfigurer {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Bean
    public ClientTokenInterceptor getClientTokenInterceptor() {
        return new ClientTokenInterceptor();
    }

    /**
     *  设置拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(getClientTokenInterceptor());

        // 排除配置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/manager/login_view");
        addInterceptor.excludePathPatterns("/manager/login");
        addInterceptor.excludePathPatterns(Arrays.asList("/templates/**", "/static/**"));

        // 客户端Token验证拦截器，拦截配置
        addInterceptor.addPathPatterns("/mobile/**");

        // 签名验证拦截器,开发环境忽略签名认证
        if (!"dev".equals(activeProfile)) {
            registry.addInterceptor(new ClientSignInterceptor()).addPathPatterns("/**");
        }
    }

}
